home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 14 / Mac Magazin and MacEasy Magazine CD - Issue 14.iso / Online / NewsWatcher 2.0+jp1 folder / patch source / myTools.c next >
Text File  |  1995-07-15  |  6KB  |  313 lines

  1. // NewsWatcher 2.0 "myTools.c"
  2. // Copyright (C) 1994-1995 Mizutori Tetsuya
  3. // 1994/5/24, 1994/7/15, 1995/2/8, 1995/2/21, 1995/7/14
  4.  
  5. #include "myTools.h"
  6.  
  7. // Global variables
  8.  
  9. //Boolean gOptionPressed = false;        /* true if Option Key is pressed */
  10.  
  11.  
  12. /***** Keyboard sensing *****/
  13. #ifdef COMMENT
  14.  
  15.     Apple Standard keyboard Layout
  16.       Examples:
  17.     Code    Legend
  18.     0x00    A
  19.     0x01    S
  20.     0x06    Z
  21.     0x24    Return
  22.     0x30    Tab
  23.     0x31    Space
  24.     0x33    Delete
  25.     0x35    Escape
  26.  
  27. #endif /* COMMENT */
  28.  
  29. //    k =    keyboard scan code (0-127 or modifier keys)
  30. static Boolean IsPressed ( unsigned short k )
  31. {
  32.     unsigned char    km[16];
  33.  
  34.     switch ( k ) {
  35.         case cmdKey:        k = 0x37; break;
  36.         case shiftKey:        k = 0x38; break;
  37.         case alphaLock:        k = 0x39; break;
  38.         case optionKey:        k = 0x3A; break;
  39.         case controlKey:    k = 0x3B; break;
  40.         default:            break;
  41.     }
  42.  
  43.     GetKeys( (void *) km );
  44.     
  45.     return  ( ( ( km[k>>3] >> (k & 7) ) ) & 1 != 0 );
  46. }
  47.  
  48.  
  49. /**** Kanji code conversion *****/
  50. #ifdef COMMENT
  51.  
  52.     Kanji code conversion
  53.     Added by Mizutori Tetsuya, 1994/5/24
  54.     JIS Sequence
  55.     Kanji-in:    ESC + '$' + { 'B', '@', '(B' }
  56.     Kanji-out:    ESC + '(' + { 'J', 'B', 'H' }
  57.  
  58. #endif /* COMMENT */
  59.  
  60. #define IN(x,a,b) (((a) <= (x)) && ((x) <= (b)))
  61.  
  62.  
  63. /**** Kanji code check routines *****/
  64. Boolean IsJIS ( unsigned short d )
  65. {
  66.     unsigned short    d1, d2;
  67.  
  68.     d1 = BYTE_AT( &d, 0 );
  69.     d2 = BYTE_AT( &d, 1 );
  70.  
  71.     if ( ! IN(d1,0x21,0x7e) )  return  false;
  72.     if ( ! IN(d2,0x21,0x7e) )  return  false;
  73.  
  74.     return  true;
  75. }
  76.  
  77. Boolean IsSJIS ( unsigned short d )
  78. {
  79.     unsigned short    d1, d2;
  80.  
  81.     d1 = BYTE_AT( &d, 0 );
  82.     d2 = BYTE_AT( &d, 1 );
  83.  
  84.     if ( ! IN(d1,0x81,0x9e) && ! IN(d1,0xe0,0xec) )  return  false;
  85.     if ( ! IN(d2,0x40,0x7e) && ! IN(d2,0x80,0xfc) )  return  false;
  86.  
  87.     return  true;
  88. }
  89.  
  90. Boolean IsEUC ( unsigned short d )
  91. {
  92.     return ( ( d & 0x8080 ) == 0x8080 );
  93. }
  94.  
  95. Boolean IsKanji ( char *p, char *pStart, char *pEnd )
  96. {
  97.     if ( p < pStart ) return false;
  98.     if ( pEnd < p+2 ) return false;
  99.     
  100.     return IsSJIS( WORD_AT( p ) );
  101. }
  102.  
  103.  
  104. /***** Kanji code conversion *****/
  105.  
  106. unsigned short EUC2JIS ( unsigned short d )
  107. {
  108.     return ( d & 0x7f7f );
  109. }
  110.  
  111. unsigned short JIS2EUC ( unsigned short d )
  112. {
  113.     return ( d | 0x8080 );
  114. }
  115.  
  116.  
  117. unsigned short SJIS2JIS ( unsigned short d )
  118. {
  119.     unsigned short    d1,d2;
  120.     unsigned short    x1,x2;
  121.     unsigned short    x;
  122.  
  123.     d1 = BYTE_AT( &d, 0 );
  124.     d2 = BYTE_AT( &d, 1 );
  125.  
  126. //    if ( d1 == 0 ) return  d;
  127.  
  128.     x1 = d1 + d1 - ((d1 <= 0x9f) ? 0x00e1 : 0x0161);
  129.     if ( d2 < 0x9f ) x2 = d2 - ((d2 > 0x7f) ? 0x20 : 0x1f);
  130.     else { x2 = d2 - 0x7e; x1 += 1; }
  131.  
  132.     BYTE_AT( &x, 0 ) = x1;
  133.     BYTE_AT( &x, 1 ) = x2;
  134.  
  135.     return  x;    
  136. }
  137.  
  138. unsigned short JIS2SJIS ( unsigned short d )
  139. {
  140.     unsigned short    d1,d2;
  141.     unsigned short    x1,x2;
  142.     unsigned short    x;
  143.  
  144.     d1 = BYTE_AT( &d, 0 );
  145.     d2 = BYTE_AT( &d, 1 );
  146.  
  147. //    if ( d1 == 0 ) return  d;
  148.  
  149.     x1 = ((d1-1)>>1) + ((d1 <= 0x5e) ? 0x71 : 0xb1);
  150.     x2 = d2 + ((d1 & 1) ? ((d2 < 0x60) ? 0x1f : 0x20) : 0x7e);
  151.  
  152.     BYTE_AT( &x, 0 ) = x1;
  153.     BYTE_AT( &x, 1 ) = x2;
  154.  
  155.     return  x;    
  156. }
  157.  
  158.  
  159. /***** Skip puncutuations or braces following to Kanji string *****/
  160.  
  161. long SkipOnBrace ( char *p, char *pStart, char *pEnd )
  162. {
  163.     long     skipCount;
  164.     register unsigned char    *q, cp;
  165.     unsigned char    romanBraces[16] = ".,;:?!)]}>-\0";
  166.     unsigned char    kanjiBraces[64] = {
  167.         0xff,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
  168.         0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0xff,0xff,
  169.         0x60,0xff,0xff,0x63,0x64,0xff,0x66,0xff,0x68,0xff,0x6a,0xff,0x6c,0xff,0x6e,0xff,
  170.         0x70,0xff,0x72,0xff,0x74,0xff,0x76,0xff,0x78,0xff,0x7a,0xff,0xff,0xff,0xff,0x00,
  171.         };
  172.     //    The items of kanjiBraces[] may be assigned in a random order.
  173.     //    You are allowed to eliminate the items whose value is 0xff.
  174.     //    kanjiBraces[] array must have its last item as '\0'.
  175.     //    So, you can also define: kanjiBraces[] = "ABCD...XYZ[\\]`cdfhjlnprtvxz"
  176.     //    kanjiBraces[] includes punctuations and right braces in [0x8140 .. 0x817E]
  177.     //    which follow the Kanji strings.
  178.     //            0 1 2 3 4 5 6 7 8 9 A B C D E F
  179.     //    0x8140:    Å@ÅAÅBÅCÅDÅEÅFÅGÅHÅIÅJÅKÅLÅMÅNÅO    0x40:-123456789ABCDEF
  180.     //    0x8150:    ÅPÅQÅRÅSÅTÅUÅVÅWÅXÅYÅZÅ[Å\Å]Å^Å_    0x50:0123456789ABCD--
  181.     //    0x8160:    Å`ÅaÅbÅcÅdÅeÅfÅgÅhÅiÅjÅkÅlÅmÅnÅo    0x60:0--34-6-8-A-C-E-
  182.     //    0x8170:    ÅpÅqÅrÅsÅtÅuÅvÅwÅxÅyÅzÅ{Å|Å}Å~**    0x70:0-2-4-6-8-A----*
  183.     //    Sorry, these 2-byte characters are shown properly in Kanji font.
  184.  
  185. //    if ( IsPressed(optionKey) || IsPressed(shiftKey) )  return 0;
  186.  
  187.     if ( p < pStart )  return 0;
  188.     if ( pEnd <= p  )  return 0;
  189.     
  190.     if ( IsKanji(p,pStart,pEnd) ) {
  191.         if ( *p != 0x81 )  return 0;
  192.         cp = *(p+1);
  193.         q = kanjiBraces;
  194.         skipCount = 2;
  195.     } else {
  196.         cp = *p;
  197.         q = romanBraces;
  198.         skipCount = 1;
  199.     }
  200.     
  201.     while ( *q != '\0' ) {
  202.         if ( cp == *q )  return skipCount;
  203.         q++;
  204.     }
  205.  
  206.     return 0;
  207. }
  208.  
  209.  
  210. /**** Memory routines *****/
  211. #include "memutil.h"
  212.  
  213. OSErr MyResizeHandle (void *handle, Size len)
  214. {
  215.     char hState;
  216. //    Size oldLen;
  217.     OSErr err;
  218.  
  219. //    oldLen = GetHandleSize(handle);
  220.  
  221.     hState = MyHGetState(handle);
  222.     MyHUnlock(handle);
  223.  
  224.     err = MySetHandleSize(handle, len);
  225.  
  226.     MyHSetState(handle, hState);
  227.     
  228.     return err;
  229. }
  230.  
  231.  
  232. /***** Resource Handling *****/
  233. long    gFillColumn = 0;
  234.  
  235. #define kFillColumnID        800
  236. #define    iTextBox            4
  237.  
  238. #define    kMoveToFront        (WindowPtr)-1L
  239. #define    kNilFilterProc        nil
  240.  
  241. long GetFillColumn ( void )
  242. {
  243.     DialogPtr    dialog;
  244.     long        data;
  245.     
  246.     data = ( gFillColumn > 0 ? gFillColumn : 76 );
  247.  
  248.     if ( ! IsPressed( optionKey ) )  return data;
  249.  
  250.     dialog = GetNewDialog( kFillColumnID, nil, kMoveToFront );
  251.     if ( dialog != nil ) {
  252.         short    item;
  253.         short    iType;
  254.         Handle    iHndl;
  255.         Rect    iRect;
  256.         Str255    text;
  257.         OSErr    err;
  258.  
  259.         GetDialogItem( dialog, iTextBox, &iType, &iHndl, &iRect );
  260.  
  261.         NumToString( data, text );
  262.         SetDialogItemText( iHndl, text );
  263.         SelectDialogItemText( dialog, iTextBox, 0, 32767 );
  264.     
  265. //        ParamText( prompt, "\p", "\p", "\p" );
  266.     
  267.         err = SetDialogDefaultItem( dialog, ok );
  268.         err = SetDialogCancelItem( dialog, cancel );
  269.         err = SetDialogTracksCursor( dialog, true );
  270.     
  271.         ShowWindow( dialog );
  272.  
  273.         do {
  274.             ModalDialog( kNilFilterProc, &item );
  275.         } while ( item != ok && item != cancel );
  276.  
  277.         GetDialogItemText( iHndl, text );
  278.  
  279.         DisposeDialog( dialog );
  280.     
  281.         if ( item == ok )  StringToNum( text, &data );
  282.     }
  283.     
  284.     if ( data < 10 )  data = 10;
  285.  
  286.     gFillColumn = data;
  287.  
  288.     return data;
  289. }
  290.  
  291.  
  292. #ifdef COMMENT
  293. long GetFillColumn ( void )
  294. {
  295.     StringHandle    theHndl;
  296.     long            data = 0;
  297.  
  298.     theHndl = GetString( kFillColumnID );
  299.     
  300.     if ( theHndl != nil ) {
  301.         StringToNum( *theHndl, &data );
  302.         DisposeHandle( (Handle) theHndl );
  303.     }
  304.     
  305.     if ( data < 10 )  data = 10;
  306.     
  307.     return data;
  308. }
  309. #endif /* COMMENT */
  310.  
  311.  
  312. // end of program
  313.